package com.own.business.message.center.controller.user.open;

import com.own.business.message.center.business.service.MessageCenterContentService;
import com.own.business.message.center.business.service.MessageCenterGroupChildService;
import com.own.business.message.center.entity.query.MessageCenterContentQuery;
import com.own.business.message.center.entity.vo.MessageCenterContentVo;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.PageModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * UserMessageCenterContentOpen
 *
 * @author chenxueli
 * @date 2023-11-17 11:53:00
 */
@Slf4j
@Component
public class UserMessageCenterContentOpen {

    @Resource
    private MessageCenterContentService messageCenterContentService;
    @Resource
    private MessageCenterGroupChildService messageCenterGroupChildService;

    /**
     * 分页查询信息
     *
     * @param query 查询条件
     * @return 分页结果
     */
    public PageModel<MessageCenterContentVo> pageForGroupKey(MessageCenterContentQuery query) {
        var loginUser = SessionUserUtil.getLoginUser();
        query.setReceiveId(loginUser.userId());
        // 如果groupKey不为空，需要去查询groupKey对应的messageKey
        if (StringUtils.isNotBlank(query.getGroupKey())) {
            query.setGroupKey(null);
            var messageKeyList = messageCenterGroupChildService.listMessageKeyByGroupKey(query.getGroupKey());
            if (StringUtils.isNotBlank(query.getMessageKey())) {
                if (!messageKeyList.contains(query.getMessageKey())) {
                    return new PageModel<>(query);
                }
            } else {
                query.setMessageKeyList(messageKeyList);
            }
        }
        return messageCenterContentService.page(query);
    }

}
